package com.hxs.learn.action.core;

import com.hxs.learn.constants.core.web.ReqConst;
import com.hxs.learn.constants.core.web.view.ViewNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.common.web.annotation.Login;
import com.hxs.learn.entity.core.SysRole;
import com.hxs.learn.service.core.SysRoleService;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @author huxinsheng
 */
@RestController
@RequestMapping("core/role")
public class RoleAction extends AbstractViewAction {
    private final SysRoleService roleService;

    @Autowired
    public RoleAction(SysRoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation(
        value     = "删除角色数据",
        notes     = "角色管理时删除角色数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "id",
        value     = "角色ID",
        required  = true,
        dataType  = "String",
        paramType = "query"
    ) , })
    @PostMapping(ReqConst.REQ_DELETE_DATA_BY_ID)
    @ResponseBody
    public Result deleteData(@PathVariable String id) {
        return roleService.removeById(id);
    }

    /**
     * index
     *
     * @return view
     */
    @GetMapping(ReqConst.REQ_INDEX)
    public ModelAndView index(@Login LoginInfo userInfo) {
        return createModelAndView(userInfo);
    }

    @ApiOperation(
        value    = "保存角色数据",
        notes    = "角色管理时保存权限数据"
    )
    @ApiParam(
        name     = "角色对象",
        value    = "传入json格式",
        required = true
    )
    @PostMapping(ReqConst.REQ_MODIFY_DATA)
    @ResponseBody
    public Result modifyData(@Login LoginInfo loginInfo, SysRole rolePojo) {
        return roleService.save(loginInfo, rolePojo);
    }

    @ApiOperation(
        value     = "编辑角色页面",
        notes     = "角色管理时显示编辑角色页面"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "id",
        value     = "角色ID",
        dataType  = "String",
        paramType = "query"
    ) , })
    @GetMapping(ReqConst.REQ_MODIFY_DATA_DIALOG)
    public ModelAndView modifyDataDialog(Model model, String id) {
        SysRole rolePojo = roleService.findById(id);

        model.addAttribute(PAGE_DATA, rolePojo);

        return createModelAndView(ViewNames.PAGE_ROLE_MODIFY);
    }

    @ApiOperation(
        value     = "获取角色页面数据",
        notes     = "角色管理时获取角色页面数据"
    )
    @ApiImplicitParams({ @ApiImplicitParam(
        name      = "name",
        value     = "角色名称",
        dataType  = "String",
        paramType = "query"
    ) , })
    @ResponseBody
    @GetMapping(ReqConst.REQ_QUERY_PAGE_DATA)
    public Object queryPageData(String name) {
        return roleService.getRoleTreeJsonData(name);
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
